<template>
	<view>
		<!-- 搜索 -->
		<view class=""
			style="position: fixed;z-index: 99;width: 100%; height: 8vh; background: #fff; display: flex; align-items: center; padding-top: 20px; justify-content: space-around;">
			<image @click="toReturn" style="width: 50rpx; height: 50rpx;" src="../../static/images/goBack.png"></image>
			<view class=""
				style="width: 70%; height: 70rpx;border-radius: 35rpx; background:#f6f6f6 ; display: flex;align-items: center;">
				<image style="margin-left: 15rpx; width:50rpx; height: 50rpx;" src="../../static/sou.png"></image>
				<input type="text" v-model="valueName" confirm-type="search" @confirm="search" placeholder="搜索书名/作者/角色">
			</view>
			<view @click="toSearch()">
				<text>搜索</text>
			</view>
		</view>
		<!-- 搜索记录 -->
		<view class="" style="width: 90%; margin: auto; padding-top: 10vh;">
			<view class="" style="display: flex; align-items: center; justify-content: space-between;">
				<p style="font-size: 40rpx; font-weight: 600; height: 80rpx;line-height: 80rpx; ">搜索历史</p>
				<p style="font-size: 30rpx; " @click="toEmpty()">清空</p>
			</view>
			<view class="" style="display: flex;flex-wrap: wrap; align-items: center; justify-content: flex-start;">
				<view class="" style=" display: flex;width: 100px; align-items: center;justify-content: center; margin: 5px ;
				 background: #F6F6F6; border-radius: 15px;" v-for="(item,index) in history" :key="index" @click="tohistory(item)">
					<text style="white-space: nowrap; 
  overflow: hidden; 
  text-overflow: ellipsis;  font-size: 30rpx;padding: 5rpx ; ">{{item.keywords}}</text>
				</view>
			</view>


		</view>
		<!-- 热门搜索 -->
		<view class="" style="width: 90%; margin: auto;">
			<p style="font-size: 40rpx; font-weight: 600; height: 80rpx;line-height: 80rpx; ">热门搜索</p>
			<p
				style="font-size: 30rpx; height: 80rpx;font-weight: 600; background: #fffcf1; color:#d5665e ; line-height: 80rpx;">
				主编推荐</p>
			<!-- 主编推荐 -->
			<view class="" v-for="(item ,index) in tuiList" @click="toBooks(item)"
				style="display: flex; align-items: center; padding: 20rpx;">
				<view class="">
					<img style=" width: 40rpx; height: 40rpx;" :src="getMedalImage(index)">
				</view>
				<view class="" style="padding: 0 20rpx;">
					<image style="width: 150rpx; height: 200rpx;" :src="`${yourBaseURL}${item.mian_image}`"></image>
				</view>
				<view class="">
					<p style="font-weight: 600; font-size: 40rpx;">{{item.title}}</p>
					<p style="font-size: 30rpx;color: #656565;"><span>作者：</span>{{item.author}}</p>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import {
		getMytuiList,
		getSearchLog,
		getDelsearchLog
	} from "@/http/api.js"
	export default {
		data() {
			return {
				yourBaseURL: 'http://hyxs.ychlkj.cn',
				valueName: '',
				tuiList: [],
				userInfo: uni.getStorageSync('userInfo'),
				history: []
			};
		},
		onLoad() {
			this.MytuiList()

		},
		onShow() {
			this.SearchLog()
		},
		methods: {
			// 返回上一页
			toReturn() {
				uni.navigateBack();
			},


			// 排名图片
			getMedalImage(index) {
				if (index === 0) {
					return 'http://hyxs.ychlkj.cn/uploads/20231027/7efcefdb0c719fc75011c5bd599c3662.png'; // 第一名显示皇冠
				} else if (index === 1) {
					return 'http://hyxs.ychlkj.cn/uploads/20231027/87ba1364f87e24fe87f46ea171a4e5a9.png ';; // 第二名显示金色
				} else if (index === 2) {
					return 'http://hyxs.ychlkj.cn/uploads/20231027/98d85dadcad8f59c06cc68f9707e1cc4.png'; // 第三名显示银色
				} else {
					return 'http://hyxs.ychlkj.cn/uploads/20231027/ebb0b80faef41af7e9bdb1b470c3cfca.png'; // 第三名之后不显示任何标识
				}
			},
			MytuiList() {
				// let data ={

				// }
				getMytuiList().then((res) => {
					this.tuiList = res[1].data.data.tuiList
					console.log(res[1].data.data.tuiList, "推荐");
				})
			},
			toBooks(item) {
				uni.navigateTo({

					url: '/pages/books/details/details?id=' + item.id
				})
				console.log(item.id, "99999");
			},
			// 搜索
			toSearch() {
				console.log(this.valueName, "【】【】【】");
				if (this.valueName !== '') {
					uni.navigateTo({
						url: './search_news/search_news?name=' + this.valueName,
					});
					this.valueName =''
				}

			},
			search() {

				this.toSearch()
			},
			// 搜索历史
			SearchLog() {
				let data = {

					"token": this.userInfo.userinfo.token
				}
				getSearchLog(data).then((res) => {
					this.history = res[1].data.data
					console.log(res[1].data.data, "搜索历史");
				})
			},
			// 点击清空·
			toEmpty() {
				this.DelsearchLog()
			},
			// 清空历史记录
			DelsearchLog() {
				let data = {
					"token": this.userInfo.userinfo.token
				}
				getDelsearchLog(data).then((res) => {
					console.log(res[1].data.data, "清空历史记录");
					this.SearchLog()
				})
			},
			// 点击搜索记录
			tohistory(item){
				console.log(item,"【");
				uni.navigateTo({
					url: './search_news/search_news?name=' +item.keywords
				});
				this.valueName =''
			}
		},
	}
</script>

<style lang="scss">

</style>